package com.amor.ui

import android.os.SystemClock
import android.view.ViewGroup
import android.webkit.WebSettings
import com.amor.app.MyApplication
import com.amor.widget.ExWebView
import com.hy.frame.util.LogUtil


class PreWebActivity : BaseWebActivity() {

    private var mWeb: ExWebView? = null

    override fun initView() {
        super.initView()
        initStartTime = SystemClock.elapsedRealtime()
        LogUtil.d(TAG, "初始化WebView start")
        mWeb = MyApplication.get().getWebViewHolder().build(WEB_FLAG)
        initWebSettings(mWeb!!.settings, WebSettings.LOAD_DEFAULT)
        initClient(mWeb!!)
        mBinding?.container?.addView(mWeb, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        LogUtil.d(TAG, "初始化WebView end time=%sms".format(SystemClock.elapsedRealtime() - initStartTime))
        showConsole("初始化WebView耗时=%sms\n".format(SystemClock.elapsedRealtime() - initStartTime))
    }

    override fun initData() {
        super.initData()
        if (mWeb == null)
            return
        if (!mWeb!!.isPrePageFinished(URL, true)) {
            mWeb!!.loadUrl(URL)
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }

    override fun onDestroy() {
        super.onDestroy()
        LogUtil.d(TAG, "onDestroy")
        //这里不清理
        //mWeb?.clear()
        mWeb?.clearHistory()
        mWeb?.strip()
    }

    companion object {
        const val URL = "https://m.huize.com/zhishi/tuijian/0"
        const val WEB_FLAG = "pre_web"
    }
}